#include "DbSetting.h"
#include "dbapp.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>

using namespace ns_yht;

DbSetting::DbSetting()
{
}

DbSetting::~DbSetting()
{
}

bool DbSetting::checkTSetting()
{
    if (tableOk)
        return true;
    if (!DBAPP || !DATABASE.isOpen())
        return false;
    QSqlQuery query(DATABASE);
    QString strsql = QString::fromLocal8Bit("create table if not exists TSetting(ID INTEGER NOT NULL PRIMARY KEY, svalue TEXT);");
    if (!query.exec(strsql))
    {
        QSqlError str = query.lastError();
        QString strerror = str.text();
        qDebug() << "checkTSetting.lastError()----------------------" << strerror;
        return false;
    }

    tableOk = true;
    return true;
}

QString DbSetting::getSetting(int id)
{
    if (!checkTSetting())
        return "";
    if (!DBAPP || !DATABASE.isOpen())
        return "";
    QSqlQuery query(DATABASE);

    QString svalue;
    query.prepare("select svalue from TSetting where id=:id");
    query.bindValue(":id", id);

    if (!query.exec())
    {
        qDebug() << "getSetting--" << id << query.lastError();
        return "";
    }
    else
    {
        if (query.next())
        {
            svalue = query.value(0).toString();
        }
        return svalue;
    }
}

bool DbSetting::getSettings(QMap<int, QString> &valueList)
{
    if (!checkTSetting())
        return false;
    if (!DBAPP || !DATABASE.isOpen())
        return false;
    QSqlQuery query(DATABASE);
    query.prepare("select id,svalue from TSetting");

    if (!query.exec())
    {
        qDebug() << "getSettings----111---" << query.lastError();
        return false;
    }
    valueList.clear();
    while (query.next())
    {
        valueList[query.value("id").toInt()] = query.value("svalue").toString();
    }

    return true;
}

bool DbSetting::getSettings(QMap<int, QString> &valueList, QVector<int> ids)
{
    if (!checkTSetting())
        return false;
    if (!DBAPP || !DATABASE.isOpen())
        return false;
    QSqlQuery query(DATABASE);
    QString str = "select id,svalue from TSetting";
    if (ids.count() > 0)
    {
        str = str + " where ";
        for (int i = 0; i < ids.count(); i++)
        {
            if (i == 0)
                str = str + " id = " + QString::number(ids.at(i));
            else
                str = str + " or id = " + QString::number(ids.at(i));
        }
    }

    query.prepare(str);

    if (!query.exec())
    {
        qDebug() << "getSettings----222---" << query.lastError();
        return false;
    }
    valueList.clear();
    while (query.next())
    {
        valueList[query.value("id").toInt()] = query.value("svalue").toString();
    }

    return true;
}

bool DbSetting::saveSetting(int id, const QString &svalue)
{
    if (!checkTSetting())
        return false;
    if (!DBAPP || !DATABASE.isOpen())
        return false;
    QSqlQuery query(DATABASE);

    query.prepare("insert or replace into TSetting(id,svalue) Values(:id,:svalue)");
    query.bindValue(":id", id);
    query.bindValue(":svalue", svalue);
    if (!query.exec())
    {
        qDebug() << "saveSetting====" << query.lastError();
        return false;
    }
    return true;
}
